#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
  echo "Error: You must be root to run this script, please use root to install mysql!"
  exit 1
fi

# Check if system is 32 bit
if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then
  echo "Error:You must use 32 bit system to run this script!"
  exit 1
fi
echo -e "\033[7m"
echo " ********************************************************************"
echo " *      LNAMP(Linux+Nginx+Apache+MySQL+PHP) 1.0 for CentOS          *"
echo " ********************************************************************"
echo " * A tool to auto-compile & install Nginx+Apache+MySQL+PHP on Linux *"
echo " *                 For any queries please contact me.               *"
echo " *   Email:webmaster@31sky.net     Blog URL:http://www.31sky.net    *"
echo " *                                                                  *"
echo " ********************************************************************"
echo -e "\033[0m"
echo ""

# set the source url
url_eaccelerator="http://lnamp-shell.googlecode.com/files/eaccelerator-0.9.6.tar.bz2"

get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}

init()
{
#set work directory

echo "==========================="

wd="/usr/local/src"
echo "Please input the work directory:"
read -p "(Default work directory: $wd):" temp
if [ "$temp" != "" ]; then
  wd=$temp
fi
echo "==========================="

echo workdirectory="$wd"
}
install_eaccelerator()
{
echo "****************************************"
echo "*      install eaccelerator            *"
echo "****************************************"
cd $wd
if [ -s eaccelerator-0.9.6.tar.bz2 ]; then
  echo "eaccelerator-0.9.6.tar.bz2 [found]"
  else
  echo "Error: eaccelerator-0.9.6.tar.bz2 not found!!!download now......"
  wget -c http://lnamp-shell.googlecode.com/files/eaccelerator-0.9.6.tar.bz2
  echo "eaccelerator-0.9.6.tar.bz2 download finishing!"
fi
tar -jxf eaccelerator-0.9.6.tar.bz2
cd eaccelerator-0.9.6/
phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/bin/php-config
make
make install
cd ..
mkdir -p /tmp/eaccelerator
chmod 777 /tmp/eaccelerator
echo "mkdir -p /tmp/eaccelerator" >> /etc/rc.local
echo "chmod 777 /tmp/eaccelerator" >> /etc/rc.local
}
create_eaccelerator_config_file()
{
cd $wd
cat >>/etc/php.ini<<EOF
[eaccelerator]
extension = "eaccelerator.so"
eaccelerator.shm_size = 32
eaccelerator.cache_dir = "/tmp/eaccelerator"
eaccelerator.enable = 1
eaccelerator.optimizer = 0
eaccelerator.debug = 0
eaccelerator.name_space = ""
eaccelerator.check_mtime = 1
eaccelerator.filter = ""
eaccelerator.shm_max = 0
eaccelerator.shm_ttl = 7200
eaccelerator.shm_prune_period = 7200
eaccelerator.shm_only = 1
eaccelerator.compress = 0
eaccelerator.compress_level = 9
eaccelerator.keys = shm
eaccelerator.sessions = shm
eaccelerator.content = shm
EOF
}

echo "Press any key to start..."
char=`get_char`

init

install_eaccelerator

create_eaccelerator_config_file

service nginx restart